package com.slot.faraohgoldview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.media.MediaPlayer;
import android.os.Handler;
import android.util.Log;
import android.view.View;

public class Reel extends View {
	
	static final int[] SC_ANIM={8,9,11,12,13,16,13,12,11,14,15,17,15,14,11,9,10,8,7,9,7,6,3,0,0,1,2,3,4,5,8};
	private float scaleX;
	private float scaleY;
	private Bitmap[] mSimbols;
	public int marginX;
	public boolean isAnimReel=false;
	private boolean reverse=false;
	private boolean isAnimWin=false;
	private int moveY=0;
	private int deltaY=0;
	private int[] StripRegular;
	private int ReelsLength;
	private int CellData, scatterSymbol, scatterAnimFrame;
	private Handler nextframeHandler;
	private Handler reelStopHandler=new Handler();
	MediaPlayer mp_s = MediaPlayer.create(getContext(), R.raw.reelsstop);

	public Reel(Context context,int margin,int[] strip, int reelslength) {
		super(context);
        
        marginX=margin;
        ReelsLength=reelslength;
        CellData=(int)(Math.random()*20);
        StripRegular=strip;
        mSimbols=new Bitmap[ReelsLength+4];
        initReel();
	}
	
	@Override
    protected void onSizeChanged(int width, int height, int oldw, int oldh) {
		scaleX = (float) width/640;
		scaleY = (float) height/480;
	}
	
	@Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(isAnimReel) {
        	if(!reverse) {
        		if(moveY<ReelsLength*101*scaleY){
        			if(deltaY<25*scaleY) deltaY+=5*scaleY;
        			moveY+=deltaY;
        		} else {
        			reverse=true;
        			deltaY=(int) (10*scaleY);
        			moveY-=deltaY;
        		}
        		postInvalidateDelayed(1000/30);
        	} else if(reverse) {
        		if(moveY>ReelsLength*100*scaleY){
        			if(deltaY>5*scaleY) deltaY-=1*scaleY;
        			moveY-=deltaY;
        			postInvalidateDelayed(1000/30);
        		} else {
        			moveY=(int) (ReelsLength*100*scaleY); 
        			isAnimReel=false;
        			if(CellData+ReelsLength>StripRegular.length) 
        				CellData=CellData+ReelsLength-StripRegular.length;
        				else CellData+=ReelsLength;
        			
        		}
        	}
        }
        for(int i=0;i<ReelsLength+4;i++){
        	canvas.drawBitmap(mSimbols[i],marginX*scaleX,moveY-i*100*scaleY+275*scaleY, null);
        }
	}
	
	public void initReel(){
		int celldatatmp=CellData;
		for(int i=0;i<ReelsLength+4;i++){
        	if(celldatatmp+i>StripRegular.length-1) celldatatmp=-i;
        	mSimbols[i]=TopPanel.SYMBOL[StripRegular[i+celldatatmp]];
        }
	}
	
	public void startReel(){
		if(isAnimReel) return;
		isAnimReel=true;
		reverse=false;
		isAnimWin=false;
		if(nextframeHandler!=null) nextframeHandler.removeCallbacks(nextframe);
		moveY=0;
		deltaY=0;
		initReel();
		invalidate();
		
		reelStopHandler.postDelayed(StopReel, 1000);
	}
	
	public int[] getSymbols(){
		int[] symbolmatrix=new int[3];
		int celldatatmp=CellData;
		for(int i=0;i<3;i++){
			if(celldatatmp+i>StripRegular.length-1)
				celldatatmp=-i;
			symbolmatrix[i]=StripRegular[celldatatmp+i];
		}
		return symbolmatrix;
	}
	
	public void playAnimSymbol(int scatter){
		if(scatter>1){
			int celldatatmp=CellData;
			for(int i=0;i<3;i++){
				if(celldatatmp+i>StripRegular.length-1)
					celldatatmp=-i;
				if(StripRegular[celldatatmp+i]==12){
					nextframeHandler=new Handler();
					nextframeHandler.postDelayed(nextframe, 200);
					scatterSymbol=ReelsLength+i;
					scatterAnimFrame=0;
				}
			}
			isAnimWin=true;
		}
		//Log.v(">>>>>>>>>>","CellData="+mSimbols[ReelsLength-4]);
		invalidate();
	}
	
	private final Runnable nextframe = new Runnable(){
		public void run(){
			if(isAnimWin) nextframeHandler.postDelayed(nextframe, 150);
			if(scatterAnimFrame<SC_ANIM.length-1) scatterAnimFrame++;
			else scatterAnimFrame=0;
			mSimbols[scatterSymbol]=TopPanel.EYE_ANIM[SC_ANIM[scatterAnimFrame]];
			invalidate();
		}
	};
	

	
	private final Runnable StopReel = new Runnable() {
		public void run() {
			reelStopHandler.removeCallbacks(StopReel);
			if(moveY<ReelsLength*90*scaleY) reelStopHandler.postDelayed(StopReel, 200);
			else {
				mp_s.start();
			}
		}
	};

}
